package com.doit.day01;

import org.apache.kafka.clients.consumer.ConsumerConfig;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.clients.consumer.ConsumerRecords;
import org.apache.kafka.clients.consumer.KafkaConsumer;
import org.apache.kafka.common.TopicPartition;
import org.apache.kafka.common.record.TimestampType;
import org.apache.kafka.common.serialization.StringDeserializer;

import java.time.Duration;
import java.util.Arrays;
import java.util.Iterator;
import java.util.Properties;

public class ConsumerDemo {
    public static void main(String[] args) {
        Properties props = new Properties();
        //必须配置的参数
        props.setProperty(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG,"linux01:9092");
        props.setProperty(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class.getName());
        props.setProperty(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class.getName());
        props.setProperty(ConsumerConfig.GROUP_ID_CONFIG,"IDEA");

        //选配的
        //是否自动创建topic 默认是true 自动创建
        props.setProperty("allow.auto.create.topics","true");
        //从哪里读取数据  earliest 最早的  latest 最新的
        props.setProperty("auto.offset.reset","earliest");
        //是否自动提交偏移量 提交到哪去 __consumer_offsets
        props.setProperty("enable.auto.commit","true");
        //自动提交偏移量的间隔时间
        props.setProperty("auto.commit.interval.ms","10000");

        //先搞出来一个消费者对象
        KafkaConsumer<String, String> consumer = new KafkaConsumer<String, String>(props);
        //在拉取数据之前，得告诉他，读取哪一个topic中的数据
//        consumer.subscribe(Arrays.asList("d43"));
        //需求，想读取0号分区从2号偏移量开始消费
        //1号分区从3号offset开始读
        //2号分区从0号开始读offset
        TopicPartition topicPartition0 = new TopicPartition("d43", 0);
        TopicPartition topicPartition1 = new TopicPartition("d43", 1);
        TopicPartition topicPartition2 = new TopicPartition("d43", 2);
        //直接给这个消费者分配消费哪一个分区
        consumer.assign(Arrays.asList(topicPartition0,topicPartition1,topicPartition2));
        //指定从哪一个偏移量开始读
        consumer.seek(topicPartition0,2);
        consumer.seek(topicPartition1,3);
        consumer.seek(topicPartition2,0);


        while (true){
            //获取数据
            ConsumerRecords<String, String> poll = consumer.poll(Duration.ofMillis(Long.MAX_VALUE));

            //数据的业务逻辑处理 ==》 打印出来瞅瞅，我们可以获取到哪些数据
            for (ConsumerRecord<String, String> consumerRecord : poll) {
                //topic partition  offset  key   value   timestamp
                String topic = consumerRecord.topic();
                int partition = consumerRecord.partition();
                long offset = consumerRecord.offset();
                String key = consumerRecord.key();
                String value = consumerRecord.value();
                long timestamp = consumerRecord.timestamp();
                //默认：CreateTime  数据创建的时间, LogAppendTime 数据追加到日志里面的时间
                TimestampType timestampType = consumerRecord.timestampType();
                System.out.println("topic:"+topic+",partition:"+partition+",offset:"+offset
                        +",key:"+key+",value:"+value+",timestamp:"+timestamp+",timestampType:"+timestampType);
            }
        }

        //关闭资源
    }
}
